/**
 * Created with IntelliJ IDEA.
 * User: zzl
 * Date: 12-12-12
 * To change this template use File | Settings | File Templates.
 */
var jsex = {

    msie: navigator.appName === 'Microsoft Internet Explorer',

    trim: function (str) {
        var whitespace = ' \n\r\t';
        var len = str.length;
        if (len && (whitespace.indexOf(str.charAt(0)) !== -1 ||
            len > 1 && whitespace.indexOf(str.charAt(len - 1)) !== -1)) {
            return str.replace(/^\s+|\s+$/g, '');
        }
        return str;
    },

    indexOf: function (arrOrStr, elem, from) {

        if (typeof(arrOrStr) === 'string' && elem.length > 1) {
            return arrOrStr.indexOf(elem, from);
        }

        var core_indexOf = Array.prototype.indexOf;
        if (core_indexOf) {
            return core_indexOf.call(arrOrStr, elem, from);
        }
        throw new Error('NotImplemented error');
        /*
        var len = arrOrStr.length >>> 0;
        from = Number(from) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) {
            from += len;
        }
        if (typeof(arrOrStr) === 'string') {
            for (; from < len; from++) {
                if (arrOrStr.charAt(from) === elem)
                    return from;
            }
            return -1;
        }
        else { // array?
            for (; from < len; from++) {
                if (from in arrOrStr &&
                    arrOrStr[from] === elem)
                    return from;
            }
            return -1;
        }
        */
    },

    parseDate: function (str) {
        var re = new RegExp('\\s*((?:\\d{2})?\\d{2})[-/](\\d{1,2})[-/](\\d{1,2})' +
            '(?:\\s+(\\d{1,2}):(\\d{1,2})(?::(\\d{1,2}))?)?');
        var m = re.exec(str);
        if (m) {
            var date = new Date(m[1], m[2] - 1, m[3], m[4] || 0, m[5] || 0, m[6] || 0);
            return date;
        }
        return null;
    },

    /*
     * format spec:
     *  y - 2 digits year
     *  yy - 4 digits year
     *  M - month without zero
     *  MM - month with zero
     *  d - day without zero
     *  dd - day with zero
     *  h - hour without zero
     *  hh - hour with zero
     *  m - minute without zero
     *  mm - minute with zero
     *  s - second without zero
     *  ss - second with zero
     *  datetime: yy-mm-dd hh:nn:ss
     *  date: yy-mm-dd
     *  time: hh:nn:ss
     */
    formatDate: function (date, format) {
        if (format == 'datetime') {
            format = 'yy-MM-dd hh:mm:ss';
        }
        else if (format == 'date') {
            format = 'yy-MM-dd';
        }
        else if (format == 'time') {
            format = 'hh:mm:ss';
        }
        var re = /[yMdhms]{1,2}/g;

        function padZero(value, pad) {
            return value < 10 && pad ? '0' + value : String(value);
        }

        var s = format.replace(re, function (m) {
            var type = m.charAt(0);
            var doubled = m.length == 2;
            var ts;
            if (type == 'y') {
                ts = String(date.getFullYear());
                if (!doubled) {
                    ts = ts.substr(2);
                }
            }
            else if (type == 'M') {
                ts = padZero(date.getMonth() + 1, doubled);
            }
            else if (type == 'd') {
                ts = padZero(date.getDate(), doubled);
            }
            else if (type == 'h') {
                ts = padZero(date.getHours(), doubled)
            }
            else if (type == 'm') {
                ts = padZero(date.getMinutes(), doubled)
            }
            else if (type == 's') {
                ts = padZero(date.getSeconds(), doubled)
            }
            return ts || '?';
        });
        return s;
    },

    // from jQuery
    inArray: function (elem, arr, i) {
        var len;
        var core_indexOf = Array.prototype.indexOf;
        if (arr) {
            if (core_indexOf) {
                return core_indexOf.call(arr, elem, i||0) != -1;
            }

            len = arr.length;
            i = i ? i < 0 ? Math.max(0, len + i) : i : 0;

            for (; i < len; i++) {
                // Skip accessing in sparse arrays
                if (i in arr && arr[ i ] === elem) {
                    return true;
                }
            }
        }
        return false;
    },

    newRawObj: function (type) {
        if (Object.create) {
            return Object.create(type.prototype);
        }
        function f() {
        }

        f.prototype = type.prototype;
        return new f();
    },

    union: function (arr1, arr2) {
        var result = [];
        var n;
        for (n = 0; n < arr1.length; n++) {
            if (!jsex.inArray(arr1[n], result)) {
                result.push(arr1[n]);
            }
        }
        for (n = 0; n < arr2.length; n++) {
            if (!jsex.inArray(arr2[n], result)) {
                result.push(arr2[n]);
            }
        }
        return result;
    },

    distinct: function (arr) {
        var result = [];
        var n;
        for (n = 0; n < arr.length; n++) {
            if (!jsex.inArray(arr[n], result)) {
                result.push(arr[n]);
            }
        }
        return result;
    },

    replace: function(s, sFind, sReplace) {
        var findLen = sFind.length;
        var pos = 0, lastPos = 0;
        var result = '';
        while((pos = s.indexOf(sFind, pos)) != -1) {
            result += s.substring(lastPos, pos) + sReplace;
            lastPos = pos = pos + findLen;
        }
        if(lastPos) {
            result += s.substr(lastPos);
        }
		else {
			result = s;
		}
		return result;
    }


};
